package com.li.recycleradapter;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.li.interfaces.ISticky;
import com.li.utils.TransformUtil;
import com.ndboo.reslibrary.R;

/**
 * Author: Li
 * Date: 2017/10/18 0018
 * Description:
 */

public class StickyItemDecoration extends RecyclerView.ItemDecoration {

    private ISticky mISticky;
    private int mRectHeight;
    private int mTextPaintSize;
    private Paint mTxtPaint;
    private Paint mRectPaint;
    private Context mContext;
    private Paint mDividerPaint;

    public StickyItemDecoration(Context context,ISticky iSticky) {
        mISticky=iSticky;
        mContext=context;
        mRectHeight= TransformUtil.dp2px(context,30);
        mTextPaintSize=TransformUtil.dp2px(context,17);
        mTxtPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mTxtPaint.setColor(Color.BLACK);
        mTxtPaint.setTextSize(mTextPaintSize);

        mRectPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mRectPaint.setStyle(Paint.Style.FILL);
        mRectPaint.setColor(Color.parseColor("#DDDDDD"));

        mDividerPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mDividerPaint.setStyle(Paint.Style.FILL);
        mDividerPaint.setColor(ContextCompat.getColor(mContext,R.color.colorDivider));

    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount=parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view=parent.getChildAt(i);
            int left=parent.getPaddingLeft();
            int right=parent.getWidth()-parent.getPaddingRight();
            int top=view.getTop()-1;
            int bottom=view.getTop();
            c.drawRect(left,top,right,bottom,mDividerPaint);
        }
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);

        int childCount=parent.getChildCount();
        int itemCount=state.getItemCount();
        int left=parent.getPaddingLeft();
        int right=parent.getWidth()-parent.getPaddingRight();
        String preGroupTitle;
        String groupTitle="";
        for (int i = 0; i < childCount; i++) {
            View child=parent.getChildAt(i);
            int pos=parent.getChildLayoutPosition(child);
            preGroupTitle=groupTitle;
            groupTitle=mISticky.getGroupName(pos);
            if (groupTitle.equals(preGroupTitle)) {
                continue;
            }
            int textY=Math.max(mRectHeight,child.getTop());

            int viewBottom=child.getBottom();
            String title=mISticky.getGroupName(pos);
            if (pos + 1 < itemCount) {
                String nextGroupTitle=mISticky.getGroupName(pos+1);
                if (!nextGroupTitle.equals(groupTitle) && viewBottom < textY) {
                    //将上一个往上移动
                    textY = viewBottom;
                }
            }
            c.drawRect(left, textY - mRectHeight, right, textY, mRectPaint);
            float value= mTxtPaint.getFontMetrics().ascent+mTxtPaint.getFontMetrics().descent;
            c.drawText(title, left+TransformUtil.dp2px(mContext,8), textY-mRectHeight/2-value/2, mTxtPaint);

        }

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int pos=parent.getChildLayoutPosition(view);

        if (mISticky.isFirstPosition(pos)) {
            outRect.top=mRectHeight;
            outRect.bottom=1;
        }else {
            outRect.bottom=1;
        }


    }
}
